"""
FileName：conftest.py
Description：
Author：gaojiahui
Time：2025/8/29 18:39
Website：https://gitee.com/gao-jiahui
"""
from selenium import webdriver
import pytest
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions

from config.config import Config


def pytest_addoption(parser):
    parser.addoption("--browser", action="store", default=Config.BROWSER)
    parser.addoption("--headless", action="store", default=Config.HEADLESS)

@pytest.fixture(scope="session")
def browser_type(request):
    return request.config.getoption("--browser")

@pytest.fixture(scope="session")
def headless_mode(request):
    return request.config.getoption("--headless")

@pytest.fixture(scope="function")
def driver(browser_type, headless_mode):
    if browser_type == "chrome":
        options = ChromeOptions()
        if headless_mode:
            options.add_argument("--headless")
        driver = webdriver.Chrome(options=options)
    elif browser_type == "firefox":
        options = FirefoxOptions()
        if headless_mode:
            options.add_argument("--headless")
        driver = webdriver.Firefox(options=options)
    else:
        raise ValueError(f"不支持的浏览器类型: {browser_type}")

    driver.implicitly_wait(Config.TIMEOUT)
    driver.maximize_window()

    yield driver
    driver.quit()

@pytest.fixture(scope="function")
def home_page(driver):
    from pages.home_page import HomePage
    return HomePage(driver)
